package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDrugCreateDTO;
import com.sky.entity.Category;
import com.sky.entity.CategoryDrug;
import com.sky.entity.Drug;
import com.sky.exception.CategoryException;
import com.sky.mapper.CategoryDrugMapper;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DrugMapper;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryDrugDetailVO;
import com.sky.vo.CategoryDrugVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private CategoryDrugMapper categoryDrugMapper;


    /**
     * 新增药品组和
     *
     * @param categoryDrugCreateDTO
     */
    @Override
    public void addCategoryGroup(CategoryDrugCreateDTO categoryDrugCreateDTO) {
        Long userId = BaseContext.getCurrentId();
        List<Long> ids = categoryDrugCreateDTO.getIds();
        if (ids == null || ids.size() == 0) {
            throw new CategoryException(MessageConstant.GROUP_NULL);
        }
        String categoryName = categoryDrugCreateDTO.getCategoryName();
        if (categoryName == null || categoryName.length() == 0) {
            throw new CategoryException(MessageConstant.GROUP_NAME_NULL);
        }
        //插入组合表
        Category category = new Category();
        category.setCategoryName(categoryName);
        category.setUserId(userId);
        //默认是未启用状态
        category.setCategoryStatus(1);
        //todo以后再添加药品组合的图片
        categoryMapper.addCategory(category);
        //插入组合药品表信息
        categoryDrugMapper.insertCategoryGroup(category.getId(), ids);
        log.info("组合字段添加成功");
    }

    /**
     * 获取到药品组合的列表信息
     *
     * @return
     */
    public List<CategoryDrugVO> getCategoryList() {
        Long userId = BaseContext.getCurrentId();
        List<CategoryDrugVO> categoryDrugVOList = new ArrayList<>();
        //根据用户id获取药品的集合
        List<CategoryDrug> categoryDrugs = categoryDrugMapper.getCategoryDrugsByUserId(userId);
        log.info("组合药品的信息：{}", categoryDrugs);
        for (CategoryDrug categoryDrug : categoryDrugs) {
            CategoryDrugVO categoryDrugVO = new CategoryDrugVO();
            //获取到当前的药品组合信息

            Category category = categoryMapper.getCategoryById(categoryDrug.getCategoryId());

            BeanUtils.copyProperties(category, categoryDrugVO);
            String orderDishesStr = getDrugsStr(categoryDrug);
            log.info("药品转换从字符串后：{}", orderDishesStr);
            categoryDrugVO.setDrugs(orderDishesStr);
            log.info("每条药品组合信息:{}", categoryDrugVO);
            categoryDrugVOList.add(categoryDrugVO);
        }
        log.info("最终展示信息：{}", categoryDrugVOList);
        return categoryDrugVOList;
    }

    /**
     * 获取组合药品的详情
     *
     * @param id 组合id
     * @return
     */
    @Override
    public List<CategoryDrugDetailVO> getCategoryDrugDetail(Long id) {
        List<CategoryDrugDetailVO> categoryDrugDetailVOList = new ArrayList<>();
        //获取到组合药品
        categoryDrugMapper.getDrugIdByCategoryId(id);
        return null;
    }


    private String getDrugsStr(CategoryDrug category) {
        // 获取到当前组合对应的所有的药品id
        List<Long> drugIds = categoryDrugMapper.getDrugIdByCategoryId(category.getId());
        if (drugIds == null || drugIds.isEmpty()) {
            return "";
        }
        // 根据ids批量获取所有药品信息
        List<Drug> drugs = drugMapper.getListByIds(drugIds);

        // 将每一条订单菜品信息拼接为字符串（格式：药品名称:频率；）
        List<String> orderDishList = drugs.stream().map(x -> {
            return x.getDrugName() + ":" + x.getFrequency() + " " + x.getDrugDosage() + ";";
        }).collect(Collectors.toList());
        // 将该订单对应的所有菜品信息拼接在一起
        return String.join("", orderDishList);
    }
}
